package com.dongdongshop.dao;

import com.dongdongshop.pojo.evaluate.Evaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/4/3 21:11
 */
@Component
public class EvaluateDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加评论
     * @param evaluate
     */
    public void addEvaluate(Evaluate evaluate) {
        mongoTemplate.save(evaluate);
    }


    /**
     * 通过goodsid查询所有评论
     * @return
     */
    public List<Evaluate> findByGoodsId(Long goodsId) {
        Criteria criteria = Criteria.where("goodsId").is(goodsId);
        Query query = Query.query(criteria);
        List<Evaluate> evaluates = mongoTemplate.find(query, Evaluate.class);
        return evaluates;
    }

    /**
     * 评论点赞
     * @param id
     */
    public void incLikeNum(Integer id) {
        //查询条件
        Query query = Query.query(Criteria.where("id").is(id));
        //修改条件 点赞数自增+1
        Update update = new Update();
        update.inc("likeNum");
        mongoTemplate.updateFirst(query,update,Evaluate.class);
    }
}
